package com.openflag.bwicservice.controller;

import com.openflag.bwicservice.domain.*;
import com.openflag.bwicservice.service.LoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

import static com.openflag.bwicservice.domain.ResultStatus.*;
import static com.openflag.bwicservice.util.ResultUtil.*;


@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/client")

public class LoginController {


    @Autowired
    private LoginService loginService;

//    @GetMapping("/login/{clientId}")
//    public Result<Client> login(@PathVariable Long clientId){
//        Client client   = loginService.getClient(clientId);
//        Result<Client> result = null;
//
//        if(client != null){
//            result = success(client);
//        }else{
//            result = fail(ResultStatus.USER_NOT_FOUND);
//        }
//        return result;
//
//
//
//    }
    @PostMapping("/login")
    public Result<Client> login(@RequestBody LoginRequest loginRequest){

        Optional<Client> optionalClient = loginService.login(loginRequest);


        Result<Client> result = null;
        if (optionalClient.isPresent()){
            result = success(optionalClient);

        }else{
            result = fail(ResultStatus.ACCOUNT_LOGIN_ERROR);
        }

        return result;


    }


    // 输入注册信息，返回状态码，0是创建成功，1是用户名重名，2是用户名不符合规则，
    // 3是密码不符合规则，4是身份验证失败,5姓名不符合规则。
    @PostMapping("/register")
    public Result<Client> register(@RequestBody RegisterRequest registerRequest){

        Integer status = loginService.register(registerRequest);

        Result<Client> feedback = null;

        switch (status){
            case 0:
                feedback = success(null);
                break;
            case 1:
                feedback = fail(REGISTER_SAME_ACCOUNT);
                break;
            case 2:
                feedback = fail(REGISTER_WRONG_ACCOUNT);
                break;
            case 3:
                feedback = fail(REGISTER_WRONG_PASSWORD);
                break;

            case 4:
                feedback = fail(REGISTER_WRONG_POSITION);
                break;

            case 5:
                feedback = fail(REGISTER_WRONG_NAME);
                break;

            default:
                feedback = fail(404,"Not Found");
        }
        return feedback;





    }
}
